import { BaseResponse } from '@/common/BaseResponse';
import { Carousel } from '@/entity/carousel.entity';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { PageParamDto } from './dto/page-param.dto';

@Injectable()
export class CarouselService {
  constructor(
    @InjectRepository(Carousel)
    private readonly carouselRepository: Repository<Carousel>,
  ) {}

  async carouselListSelect(pageParamDto: PageParamDto) {
    const qb = this.carouselRepository.createQueryBuilder('dictionaryWords');
    const result = qb;

    result
      .limit(pageParamDto.size)
      .offset((pageParamDto.page - 1) * pageParamDto.size);
    const total = await result.getCount();
    const list = await result.getMany();

    return BaseResponse.toSuccesJustData({
      page: Number(pageParamDto.page),
      list,
      size: Number(pageParamDto.size),
      total,
    });
  }

  deleteCarousel(id: number) {
    return this.carouselRepository.delete(id);
  }
}
